{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Long Pressed Name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isLongPressedName"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #长按键入"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>你的朋友正在使用键盘输入他的名字&nbsp;<code>name</code>。偶尔，在键入字符&nbsp;<code>c</code>&nbsp;时，按键可能会被<em>长按</em>，而字符可能被输入 1 次或多次。</p>\n",
    "\n",
    "<p>你将会检查键盘输入的字符&nbsp;<code>typed</code>。如果它对应的可能是你的朋友的名字（其中一些字符可能被长按），那么就返回&nbsp;<code>True</code>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>name = \"alex\", typed = \"aaleex\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>'alex' 中的 'a' 和 'e' 被长按。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>name = \"saeed\", typed = \"ssaaedd\"\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>'e' 一定需要被键入两次，但在 typed 的输出中不是这样。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= name.length, typed.length &lt;= 1000</code></li>\n",
    "\t<li><code>name</code> 和&nbsp;<code>typed</code>&nbsp;的字符都是小写字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [long-pressed-name](https://leetcode.cn/problems/long-pressed-name/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [long-pressed-name](https://leetcode.cn/problems/long-pressed-name/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"alex\"\\n\"aaleex\"', '\"saeed\"\\n\"ssaaedd\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        l=r=0\n",
    "        len1,len2=len(name),len(typed)\n",
    "        while r<len2:\n",
    "            if l<len1 and name[l]==typed[r]:\n",
    "                l+=1\n",
    "                r+=1\n",
    "            elif r>0 and typed[r]==typed[r-1]:\n",
    "                r+=1\n",
    "            else:\n",
    "                return False\n",
    "        return l==len1            \n",
    "            \n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if name == typed:\n",
    "            return True\n",
    "        if not name or not typed:\n",
    "            return False\n",
    "        if name[0] == typed[0]:\n",
    "            return self.isLongPressedName(name[1:], typed[1:]) or self.isLongPressedName(name, typed[1:])\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i,j=0,0\n",
    "        while j<=len(typed)-1:\n",
    "            if i<=len(name)-1 and name[i]==typed[j]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "            elif j>0 and typed[j]==typed[j-1]:\n",
    "                j+=1\n",
    "            else:\n",
    "                return False\n",
    "        return i==len(name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i=j=0\n",
    "        while j < len(typed):\n",
    "            if i < len(name) and typed[j] == name[i]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif j > 0 and typed[j] == typed[j - 1]:\n",
    "                j += 1\n",
    "            else:\n",
    "                return False\n",
    "        return i == len(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(name) > len(typed):\n",
    "            return False\n",
    "        i = 0\n",
    "        j = 0\n",
    "        while i < len(name) and j < len(typed):\n",
    "            count = 1\n",
    "            while i + 1 < len(name) and name[i + 1] == name[i]:\n",
    "                i += 1\n",
    "                count += 1\n",
    "            if typed[j] != name[i]:\n",
    "                return False\n",
    "            while j + 1 < len(typed) and typed[j + 1] == typed[j]:\n",
    "                j += 1\n",
    "                count -= 1\n",
    "            count -= 1\n",
    "            if count > 0:\n",
    "                return False\n",
    "            i += 1\n",
    "            j += 1\n",
    "        if i < len(name) or j < len(typed):\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        n = list(name)\n",
    "        t = list(typed)\n",
    "        left = 0 #name中的指针\n",
    "        right =0 #typed中的指针\n",
    "        while right <len(typed):\n",
    "            if left < len(n) and name[left] == typed[right]:\n",
    "                left +=1\n",
    "                right +=1\n",
    "            elif right > 0 and typed[right] == name[left - 1]:\n",
    "                right +=1\n",
    "            else:\n",
    "                return False\n",
    "        return left == len(name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        n=len(name)\n",
    "        if(name[0]!=typed[0]):\n",
    "            return False\n",
    "        i,j=1,1\n",
    "        while i<n and j<len(typed):\n",
    "            if(name[i]==typed[j]):\n",
    "                i,j=i+1,j+1\n",
    "            elif(typed[j]==typed[j-1]):\n",
    "                j=j+1\n",
    "            else:\n",
    "                return False\n",
    "        if i==n:\n",
    "            while(j<len(typed)):\n",
    "                if(typed[j]!=typed[j-1]):\n",
    "                    return False\n",
    "                j=j+1\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i,j=0,0\n",
    "        while j<len(typed):\n",
    "            if i<len(name) and typed[j]==name[i]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "            elif i>0 and typed[j]==name[i-1]:\n",
    "                j+=1\n",
    "            else:\n",
    "                return False\n",
    "        return i==len(name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        n1 = len(name)\n",
    "        n2 = len(typed)\n",
    "        if n2 < n1:\n",
    "            return False\n",
    "        p1 = 0\n",
    "        p2 = 0\n",
    "        while p1 < n1 or p2 < n2:\n",
    "            try:\n",
    "                if p1 >= n1 or name[p1] != typed[p2]:\n",
    "                    if p2 == 0:\n",
    "                        return False\n",
    "                    elif typed[p2] != typed[p2 - 1]:\n",
    "                        return False\n",
    "                    else:\n",
    "                        p2 += 1\n",
    "                else:\n",
    "                    p1 += 1\n",
    "                    p2 += 1\n",
    "            except:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        # 双指针 -- 看完题解简化版本\n",
    "        # name → N1\n",
    "        # typed →N2\n",
    "        N1 ,  N2 = 0 , 0\n",
    "        while N2 < len(typed):          # 这里只考虑N2 是因为若返回True 则必须 N2>=N1\n",
    "            if N1<len(name) and name[N1]==typed[N2] : # 防止N1溢出，当二者相等,均+1\n",
    "                N1 += 1\n",
    "                N2 += 1\n",
    "            elif N2>0 and typed[N2]==typed[N2-1]:   # 考虑跟上一个相等的时候，要考虑List仅有一个数时候[0] ==[0-1]\n",
    "                N2 +=1\n",
    "            else :\n",
    "                return False\n",
    "        return N1 == len(name)  #防止出现 len(name) > len(typed)出现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        cur, n = 0, len(name)\n",
    "        for l in typed:\n",
    "            if cur == n or name[cur] != l:\n",
    "                if cur == 0 or name[cur - 1] != l: return False\n",
    "            else: cur += 1\n",
    "        return cur == n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i = 0\n",
    "        name += '1'\n",
    "        for s in typed:\n",
    "            if s == name[i]:\n",
    "                i += 1\n",
    "            elif s != name[i-1]:\n",
    "                return False\n",
    "        return i == len(name) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        left, right = 0, 0\n",
    "        while right < len(typed):\n",
    "            if name[left] == typed[right]:\n",
    "                if left == len(name)-1:\n",
    "                    if right == len(typed)-1:\n",
    "                        return True\n",
    "                    else:\n",
    "                        right += 1\n",
    "                else:\n",
    "                    left += 1\n",
    "                    right += 1\n",
    "            elif name[left] != typed[right] and right >0 and typed[right] == typed[right-1]:\n",
    "                right += 1\n",
    "            else:\n",
    "                return False\n",
    "        return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i = j = 0\n",
    "        while(i<len(name) and j<len(typed)):\n",
    "        # If the current letter matches, move as far as possible\n",
    "            if typed[j]==name[i]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                if j == 0:\n",
    "                    return False\n",
    "                while j <len(typed)-1 and typed[j] == typed[j-1]:\n",
    "                    j += 1\n",
    "                if typed[j] == name[i]:\n",
    "                    i += 1\n",
    "                    j += 1\n",
    "                else:\n",
    "                    return False\n",
    "        if i < len(name):\n",
    "            return False\n",
    "        while j < len(typed):\n",
    "            if typed[j] == typed[j-1]:\n",
    "                j += 1\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        l,r=0,0\n",
    "        while l<len(name) and r<len(typed):\n",
    "            target=name[l]\n",
    "            pre_l,pre_r=l,r\n",
    "            while l<len(name) and name[l]==target:\n",
    "                l+=1\n",
    "            if typed[r]!=target:\n",
    "                return False\n",
    "            while r<len(typed) and typed[r]==target:\n",
    "                r+=1\n",
    "            if l-pre_l>r-pre_r:\n",
    "                return False\n",
    "        return l==len(name) and r==len(typed)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        l,r=0,0\n",
    "        while l<len(name) and r<len(typed):\n",
    "            target=name[l]\n",
    "            pre_l,pre_r=l,r\n",
    "            while l<len(name) and name[l]==target:\n",
    "                l+=1\n",
    "            if typed[r]!=target:\n",
    "                return False\n",
    "            while r<len(typed) and typed[r]==target:\n",
    "                r+=1\n",
    "            if l-pre_l>r-pre_r:\n",
    "                return False\n",
    "        return l==len(name) and r==len(typed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# https://leetcode.cn/problems/long-pressed-name/description/\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        nl, tl = len(name), len(typed)\n",
    "        i, j = 0, 0\n",
    "        while i < nl:\n",
    "            duplicated = 1\n",
    "            while i+1 < nl and name[i] == name[i+1]:\n",
    "                i += 1\n",
    "                duplicated += 1\n",
    "            found = 0\n",
    "            while j < tl and name[i] == typed[j]:\n",
    "                j += 1\n",
    "                found += 1\n",
    "            if found < duplicated:\n",
    "                return False\n",
    "            i += 1\n",
    "        return True if j == tl else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        j = 0\n",
    "        for i in range(len(typed)):\n",
    "            if j == len(name) and typed[i] == name[j-1]:\n",
    "                continue\n",
    "            elif j < len(name) and typed[i] == name[j]:\n",
    "                j += 1\n",
    "            elif j < len(name) and j >=1 and typed[i] == name[j-1]:\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        # if j == len(name):\n",
    "        #     return True\n",
    "        # else:\n",
    "        #     return False\n",
    "\n",
    "        return True if j == len(name) else False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(typed) < len(name) :return False\n",
    "        i,j=0,0\n",
    "        while i<len(name) and j<len(typed):\n",
    "            if name[i]==typed[j]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "            elif name[i]!=typed[j]:\n",
    "                if j==0:return False\n",
    "                elif typed[j]==typed[j-1]:\n",
    "                    j+=1\n",
    "                elif name[i]!=typed[j]:return False\n",
    "        if i!=len(name):return False\n",
    "        while j<len(typed):\n",
    "            if  typed[j]!=typed[j-1]:return False\n",
    "            j+=1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        stack = list(name)[::-1]\n",
    "        i = 0\n",
    "        justnow = None\n",
    "        while i < len(typed):\n",
    "            while stack and i < len(typed) and stack[-1] == typed[i]:\n",
    "                justnow = stack.pop()\n",
    "                i += 1\n",
    "            if not justnow:\n",
    "                return(False)\n",
    "            if stack and i < len(typed) and typed[i] != justnow and typed[i] != stack[-1]:\n",
    "                return(False)\n",
    "            if not stack:\n",
    "                while i < len(typed) and justnow == typed[i]:\n",
    "                    i += 1\n",
    "                return(i == len(typed))\n",
    "            i += 1\n",
    "\n",
    "            # while i < len(typed) - 1 and typed[i] == typed[i + 1]:\n",
    "            #     if stack[-1] == typed[i]:\n",
    "            #         stack.pop()\n",
    "            #     i += 1\n",
    "            # if stack[-1] != typed[i + 1]:\n",
    "            #     return(False)\n",
    "            # i += 1\n",
    "        return(True if not stack else False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        stack = list(name)[::-1]\n",
    "        i = 0\n",
    "        justnow = None\n",
    "        while i < len(typed):\n",
    "            while stack and i < len(typed) and stack[-1] == typed[i]:\n",
    "                justnow = stack.pop()\n",
    "                i += 1\n",
    "            if not justnow:\n",
    "                return(False)\n",
    "            if stack and i < len(typed) and typed[i] != justnow and typed[i] != stack[-1]:\n",
    "                return(False)\n",
    "            if not stack:\n",
    "                while i < len(typed) and justnow == typed[i]:\n",
    "                    i += 1\n",
    "                return(i == len(typed))\n",
    "            i += 1\n",
    "\n",
    "            # while i < len(typed) - 1 and typed[i] == typed[i + 1]:\n",
    "            #     if stack[-1] == typed[i]:\n",
    "            #         stack.pop()\n",
    "            #     i += 1\n",
    "            # if stack[-1] != typed[i + 1]:\n",
    "            #     return(False)\n",
    "            # i += 1\n",
    "        return(True if not stack else False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        index_1, index_2 = 0, 0\n",
    "        while index_1 < len(name) and index_2 < len(typed):\n",
    "            if name[index_1] == typed[index_2]:\n",
    "                index_1 += 1\n",
    "                index_2 += 1\n",
    "            elif index_2 > 0 and typed[index_2] == typed[index_2 - 1]:\n",
    "                index_2 += 1\n",
    "            else:\n",
    "                return False\n",
    "        while 0 < index_2 < len(typed) and typed[index_2] == typed[index_2 - 1]:\n",
    "            index_2 += 1\n",
    "        if index_1 == len(name) and index_2 == len(typed):\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        # 双指针法\n",
    "        # 使用双指针分别指向name和typed,如果name[i]==typed[j],说明是匹配了的字符，j++和i++去匹配下一个元素，当name[i]!=typed[j]，分情况讨论，如果是首元素就不匹配，直接返回False，第一个元素不可能是长按，然后后面的元素，如果name[i]!=typed[j],但typed[j-1]==typed[j],说名是长按的元素，跳过重复项，到达不重复的元素比较。不会有typed[j-1]==typed[j]，但typed[j-1]!=name[i-1]的情况，因为这样的话当name[i]!=typed[j]就直接返回False了。最后如果跳过了重复项，但还是typed[j-1]!=name[i-1](abc, aadc)，或则没有重复项(abc, adc),返回Flase\n",
    "\n",
    "        lengh_name = len(name)\n",
    "        lengh_typed = len(typed)\n",
    "        j = 0\n",
    "        i = 0\n",
    "        # 当name出界或typed出界都终止匹配（name没有匹配完，例如name:\"pyplrzzzzdsfa\" type:\"ppyypllr\"type没有匹配完，例如name:\"alex\" type:\"alexxrrrrssda\")\n",
    "        while i<lengh_name  and j < lengh_typed:\n",
    "            # 字母匹配，都前移一位\n",
    "            if name[i]==typed[j]:\n",
    "                j+=1\n",
    "                i+=1\n",
    "            # 不匹配。先判断是否是首元素不匹配，是的话，返回FLASE\n",
    "            else:\n",
    "                if j==0:\n",
    "                    return False\n",
    "                # j不为0,且j处于合法范围(ab和aaaaa,一直跳过j会出界)，且前一个元素和当前的元素相等，就是长按的元素，跳过\n",
    "                else:\n",
    "                    while j < lengh_typed and typed[j]==typed[j-1]:\n",
    "                        j+=1\n",
    "                    \n",
    "                    # 跳过后，判断当前的元素是否相等(j要符合条件，可能遍历最后一个元素时j越界(name =\"kikcxmvzi\"typed =\"kiikcxxmmvvzz\")\n",
    "                    if j < lengh_typed and name[i]==typed[j]:\n",
    "                        j+=1\n",
    "                        i+=1\n",
    "                    # 跳过长按的元素还不相等，直接返回False，或则前面没有长按，单纯不相等(ab 和ac),也直接返回False\n",
    "                    else:\n",
    "                        return False\n",
    "\n",
    "        # 最后如果name没有遍历完，返回Flase\n",
    "        if i < lengh_name:\n",
    "            return False\n",
    "        \n",
    "        # 如果typed没有遍历完，就判断后面的元素是否等于name的最后元素(即jieshouh),不是则不为长按\n",
    "        while j < lengh_typed:\n",
    "            if typed[j]!=typed[j-1]:\n",
    "                return False\n",
    "            j+=1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 时间复杂度：O(N+M)，其中 M,N 分别为两个字符串的长度。\n",
    "# 空间复杂度：O(1)。\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i, j = 0 , 0\n",
    "\n",
    "\n",
    "        while j < len(typed):\n",
    "            # 两者相同，且name还没匹配完，继续向后对比\n",
    "            if i < len(name) and name[i] == typed[j]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            # 不是第一位不相同，让j移动到重复项之后的位置\n",
    "            elif j > 0 and typed[j] == typed[j-1]:\n",
    "                j += 1\n",
    "            # 包括第一位就不同、name匹配完之后typed后续有非重复项、重复项之后的typed与要匹配的name不一致\n",
    "            else:\n",
    "                return False\n",
    "        \n",
    "        # 如果 i=name.length，说明 name 的每个字符都被「匹配」了\n",
    "        return i == len(name) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(typed) < len(name) :return False\n",
    "        i=j=0\n",
    "        while i<len(name) and j<len(typed):\n",
    "            if name[i]==typed[j]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "            elif name[i]!=typed[j]:\n",
    "                if j==0:return False\n",
    "                elif typed[j]==typed[j-1]:\n",
    "                    j+=1\n",
    "                elif name[i]!=typed[j]:return False\n",
    "        if i!=len(name):return False\n",
    "        while j<len(typed):\n",
    "            if  typed[j]!=typed[j-1]:return False\n",
    "            j+=1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        len_name=len(name)\n",
    "        len_typed=len(typed)\n",
    "        i=0\n",
    "        j=0\n",
    "        if len_name>len_typed: #name长于typed一定不对\n",
    "            return False\n",
    "        else:\n",
    "            while i<len_name and j<len_typed:\n",
    "                if name[i]==typed[j]:\n",
    "                    i+=1\n",
    "                    j+=1\n",
    "                elif j>0 and name[i]!=typed[j] and typed[j] == typed[j-1]:\n",
    "                    j+=1\n",
    "                else:\n",
    "                    return False\n",
    "            if i<len_name:#name还没有遍历完\n",
    "                return False\n",
    "            else: #继续遍历完并且多出的部分应该都相同等于name最后一个字母\n",
    "                while j<len_typed:\n",
    "                    if typed[j]==typed[j-1]==name[-1]:\n",
    "                        j+=1\n",
    "                    else:\n",
    "                        return False\n",
    "                return True\n",
    "                \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(typed) < len(name): return False\n",
    "        elif len(typed) == len(name) and typed != name: return False\n",
    "        i,j = 0,0\n",
    "        while i < len(name):\n",
    "            if i+1 < len(name) and name[i+1] == name[i]:\n",
    "                if name[i] == typed[j]:\n",
    "                    i += 1\n",
    "                    j += 1\n",
    "                else:\n",
    "                    return False\n",
    "            else:\n",
    "                if j >= len(typed):\n",
    "                    return False\n",
    "                if name[i] != typed[j]:\n",
    "                    return False\n",
    "                while j<len(typed) and typed[j] == name[i]:\n",
    "                    j += 1\n",
    "                i += 1\n",
    "        if j<len(typed) and typed[j]!=name[-1]:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        left, right = 0, 0\n",
    "        while left < len(name) and right < len(typed):\n",
    "            if name[left] == typed[right]:\n",
    "                left += 1\n",
    "                right += 1\n",
    "            elif right > 0 and left > 0 and typed[right] == typed[right - 1] and name[left - 1] == typed[right]:\n",
    "                right += 1\n",
    "            else:\n",
    "                return False\n",
    "        while right < len(typed):\n",
    "            if typed[right] == typed[right - 1]:\n",
    "                right += 1\n",
    "            else:\n",
    "                return False\n",
    "        print(left, right)\n",
    "        return left == len(name) and right == len(typed)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        l=r=0\n",
    "        len1=len(name)\n",
    "        len2=len(typed)\n",
    "        while r<len2:\n",
    "            if l<len1 and name[l]==typed[r]:\n",
    "                l+=1\n",
    "                r+=1\n",
    "            elif r>0 and typed[r]==typed[r-1]:\n",
    "                r+=1\n",
    "            else:\n",
    "                return False\n",
    "        return l==len1\n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "\n",
    "        left1 = 0\n",
    "        left2 = 0\n",
    "\n",
    "        while left1 < len(name) and left2 < len(typed):\n",
    "            if name[left1] == typed[left2]:\n",
    "                left1 += 1\n",
    "                left2 += 1\n",
    "            elif left2 > 0 and typed[left2-1] == typed[left2]:\n",
    "                left2 += 1\n",
    "            else:\n",
    "                return False\n",
    "        \n",
    "        while 0 < left2 < len(typed) and typed[left2] == typed[left2-1]:\n",
    "            left2 += 1\n",
    "        \n",
    "        if left1 == len(name) and left2 == len(typed):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        n=len(name)\n",
    "        m=len(typed)\n",
    "        p1=0\n",
    "        p2=0\n",
    "        while p1<n and p2<m:\n",
    "            if name[p1]!=typed[p2]:\n",
    "                return False\n",
    "            n1=1\n",
    "            n2=1\n",
    "            while p1+n1<n and name[p1+n1]==name[p1]:\n",
    "                n1+=1\n",
    "            while p2+n2<m and typed[p2+n2]==typed[p2]:\n",
    "                n2+=1\n",
    "            if n1>n2:\n",
    "                return False\n",
    "            p1+=n1\n",
    "            p2+=n2\n",
    "        return p1==n and p2==m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(name) > len(typed):\n",
    "            return False\n",
    "        else:\n",
    "            index_1 = 0\n",
    "            index_2 = 0\n",
    "            while index_1 < len(name) and index_2 < len(typed):\n",
    "                if index_2 == 0:\n",
    "                    if typed[index_2] == name[index_1]:\n",
    "                        index_1 = index_1 + 1\n",
    "                        index_2 = index_2 + 1\n",
    "                    else:\n",
    "                        return False\n",
    "                else:\n",
    "                    if typed[index_2] == name[index_1]:\n",
    "                        index_1 = index_1 + 1\n",
    "                        index_2 = index_2 + 1\n",
    "                    elif typed[index_2] == typed[index_2 - 1]:\n",
    "                        index_2 = index_2 + 1\n",
    "                    else:\n",
    "                        return False\n",
    "            if index_1 != len(name):\n",
    "                return False\n",
    "            else:\n",
    "                if index_2 == len(typed):\n",
    "                    return True\n",
    "                else:\n",
    "                    for char in typed[index_2:]:\n",
    "                        if char != name[index_1-1]:\n",
    "                            return False\n",
    "                    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i, j = 0, 0\n",
    "        while j < len(typed):  # 需要遍历完 typed 看是否全部匹配\n",
    "            # 当最后一次相等，name 先遍历到尽头\n",
    "            # 剩下的 type 若不能满足第二个 if 的 typed[j] == typed[j-1] 就会判为 False 了\n",
    "            if i < len(name) and name[i] == typed[j]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            # 不相同可能是存在连续长按，跳过这部分\n",
    "            # 并且 j > 0 的时候才可以判断长按，若是 j = 0 就不等了，就 False 了\n",
    "            elif j > 0 and typed[j] == typed[j-1]:\n",
    "                j += 1\n",
    "            else:  # 跳过长按还不相等\n",
    "                return False\n",
    "        return i == len(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        # 双指针\n",
    "        i = 0\n",
    "        j = 0\n",
    "        n = len(typed)\n",
    "        m = len(name)\n",
    "        while j < n:\n",
    "            if i < m and name[i] == typed[j]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif j > 0 and typed[j] == typed[j-1]:\n",
    "                j += 1\n",
    "            else:\n",
    "                return False\n",
    "        return i == m \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(name) > len(typed):\n",
    "            return False\n",
    "        else:\n",
    "            index_1 = 0\n",
    "            index_2 = 0\n",
    "            while index_1 < len(name) and index_2 < len(typed):\n",
    "                if index_2 == 0:\n",
    "                    if typed[index_2] == name[index_1]:\n",
    "                        index_1 = index_1 + 1\n",
    "                        index_2 = index_2 + 1\n",
    "                    else:\n",
    "                        return False\n",
    "                else:\n",
    "                    if typed[index_2] == name[index_1]:\n",
    "                        index_1 = index_1 + 1\n",
    "                        index_2 = index_2 + 1\n",
    "                    elif typed[index_2] == typed[index_2 - 1]:\n",
    "                        index_2 = index_2 + 1\n",
    "                    else:\n",
    "                        return False\n",
    "            if index_1 != len(name):\n",
    "                return False\n",
    "            else:\n",
    "                if index_2 == len(typed):\n",
    "                    return True\n",
    "                else:\n",
    "                    for char in typed[index_2:]:\n",
    "                        if char != name[index_1-1]:\n",
    "                            return False\n",
    "                    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(typed) < len(name) :return False\n",
    "        i=j=0\n",
    "        while i<len(name) and j<len(typed):\n",
    "            if name[i]==typed[j]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "            elif name[i]!=typed[j]:\n",
    "                if j==0:return False\n",
    "                elif typed[j]==typed[j-1]:\n",
    "                    j+=1\n",
    "                elif name[i]!=typed[j]:return False\n",
    "        if i!=len(name):return False\n",
    "        while j<len(typed):\n",
    "            if  typed[j]!=typed[j-1]:return False\n",
    "            j+=1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        n1, n2 = len(name), len(typed)\n",
    "        i, j = 0, 0\n",
    "        while j < n2:\n",
    "            if i < n1 and name[i] == typed[j]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif j > 0 and typed[j] == typed[j - 1]:\n",
    "                j += 1\n",
    "            else:\n",
    "                return False\n",
    "        return i == n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "         i= 0\n",
    "         j = 0\n",
    "         while j < len(typed):\n",
    "             if i<len(name) and name[i] == typed[j]:\n",
    "                 i+=1\n",
    "                 j+=1\n",
    "             elif j>0 and typed[j] == name[i-1]:\n",
    "                j+=1\n",
    "             else:\n",
    "                return False\n",
    "         return i == len(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i = 0\n",
    "        name += '1'\n",
    "        for t in typed:\n",
    "            if t == name[i]:\n",
    "                i += 1\n",
    "            elif t != name[i-1]:\n",
    "                return False\n",
    "        return i == len(name) -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i = 0\n",
    "        j = 0\n",
    "        count_n = 0\n",
    "        count_t = 0\n",
    "        last_str_n = name[0]\n",
    "        last_str_t = typed[0]\n",
    "        while i < len(name) and j < len(typed):\n",
    "            if name[i] == typed[j]:\n",
    "                last_str_n = name[i]\n",
    "                last_str_t = typed[j]\n",
    "                count_n = 0\n",
    "                count_t = 0\n",
    "                while i < len(name) and name[i] == last_str_n:\n",
    "                    i += 1\n",
    "                    count_n += 1\n",
    "                    \n",
    "\n",
    "                while j < len(typed) and typed[j] == last_str_t:\n",
    "                    j += 1\n",
    "                    count_t += 1\n",
    "                \n",
    "                if count_n > count_t:\n",
    "                    return False\n",
    "                else:\n",
    "                    if i == len(name) and j == len(typed):\n",
    "                        break\n",
    "                    elif i == len(name) and j < len(typed) or i < len(name) and j == len(typed):\n",
    "                        return False\n",
    "                    else:\n",
    "                        continue\n",
    "            else:\n",
    "                return False\n",
    "        return True    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        p, q = 0, 0\n",
    "        while q < len(typed):\n",
    "            if p < len(name) and name[p] == typed[q]:\n",
    "                p += 1\n",
    "                q += 1\n",
    "            elif q > 0 and typed[q] == typed[q-1]:\n",
    "                q += 1\n",
    "            else:\n",
    "                return False\n",
    "        return p == len(name) # name 比 typed长的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        index_1, index_2 = 0, 0\n",
    "        while index_1 < len(name) and index_2 < len(typed):\n",
    "            if name[index_1] == typed[index_2]:\n",
    "                index_1 += 1\n",
    "                index_2 += 1\n",
    "            elif index_2 > 0 and typed[index_2] == typed[index_2 - 1]:\n",
    "                index_2 += 1\n",
    "            else:\n",
    "                return False\n",
    "        while 0 < index_2 < len(typed) and typed[index_2] == typed[index_2 - 1]:\n",
    "            index_2 += 1\n",
    "        if index_1 == len(name) and index_2 == len(typed):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        na=list(name)\n",
    "        ty=list(typed)\n",
    "        if na[0]!=ty[0]:\n",
    "            return False\n",
    "        l,r=1,1\n",
    "        while l<len(na) and r<len(ty):\n",
    "            if na[l]==ty[r]:\n",
    "                l+=1\n",
    "                r+=1\n",
    "            elif ty[r]==ty[r-1]:\n",
    "                r+=1\n",
    "            else:\n",
    "                return False\n",
    "        \n",
    "        if l<len(na):\n",
    "            return False\n",
    "        else:\n",
    "            while r<len(ty):\n",
    "                if ty[r]==ty[r-1]:\n",
    "                    r+=1\n",
    "                else:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if name is typed:\n",
    "            return True\n",
    "        if len(typed) == 1:\n",
    "            return name is typed\n",
    "        if len(typed) < len(name):\n",
    "            return False\n",
    "        if name[0] != typed[0]:\n",
    "            return False\n",
    "        i = 0\n",
    "        j = 0\n",
    "        flag = True\n",
    "        while i < len(name):\n",
    "            if j < len(typed):\n",
    "                if name[i] == typed[j]:\n",
    "                    i += 1\n",
    "                    j += 1\n",
    "                elif name[i] != typed[j]:\n",
    "                    if typed[j] == typed[j - 1]:\n",
    "                        j += 1\n",
    "                    else:\n",
    "                        flag = False\n",
    "                        break\n",
    "            else:\n",
    "                flag = False\n",
    "                break\n",
    "        while j < len(typed):\n",
    "            if typed[j] == name[-1]:\n",
    "                j += 1\n",
    "            else:\n",
    "                flag = False\n",
    "                break\n",
    "        return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        l,r=0,0\n",
    "        while l<len(name) and r<len(typed):\n",
    "            target=name[l]\n",
    "            pre_l,pre_r=l,r\n",
    "            while l<len(name) and name[l]==target:\n",
    "                l+=1\n",
    "            if typed[r]!=target:\n",
    "                return False\n",
    "            while r<len(typed) and typed[r]==target:\n",
    "                r+=1\n",
    "            if l-pre_l>r-pre_r:\n",
    "                return False\n",
    "        return l==len(name) and r==len(typed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        left1=0\n",
    "        left2=0\n",
    "        while left1<len(name) and left2<len(typed):\n",
    "            if name[left1]==typed[left2]:\n",
    "                left1 +=1\n",
    "                left2 +=1\n",
    "            elif left2>0 and  typed[left2-1]==typed[left2]:\n",
    "                left2 +=1\n",
    "            else:\n",
    "                return False\n",
    "        \n",
    "        while 0<left2<len(typed) and typed[left2-1]==typed[left2]:\n",
    "            left2 +=1\n",
    "        if left1 ==len(name) and left2==len(typed):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if name[0] != typed[0]:  # 如果第一位不相等，直接返回 False\n",
    "            return False\n",
    "        p1 = p2 = 0\n",
    "        while p1 < len(name) and p2 < len(typed):\n",
    "            if name[p1] == typed[p2]:\n",
    "                p1 += 1\n",
    "                p2 += 1\n",
    "            else:\n",
    "                if typed[p2] == typed[p2-1]:\n",
    "                    p2 += 1\n",
    "                else:\n",
    "                    return False\n",
    "        if p2 == len(typed):\n",
    "            return True if p1 == len(name) else False\n",
    "        else:\n",
    "            return True if p1 == len(name) and all(name[-1] == ch for ch in typed[p2:]) else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "     \n",
    "        if name[0] != typed[0]:\n",
    "            return False\n",
    "\n",
    "        start1, start2 = 0, 0\n",
    "        cmp = name[0]\n",
    "\n",
    "        while start1 < len(name) and start2 < len(typed):\n",
    "            cnt1, cnt2 = 0, 0\n",
    "            \n",
    "            while start1 < len(name) and name[start1] == cmp:\n",
    "                    start1 += 1\n",
    "                    cnt1 += 1\n",
    "            \n",
    "            while start2 < len(typed) and typed[start2] == cmp:\n",
    "                    start2 += 1\n",
    "                    cnt2 += 1\n",
    "\n",
    "            if start1 < len(name) and start2 < len(typed) and name[start1] != typed[start2]:\n",
    "                return False\n",
    "\n",
    "            if cnt1 > cnt2:\n",
    "                return False\n",
    "\n",
    "            if start1 < len(name):\n",
    "                cmp = name[start1] \n",
    "            \n",
    "        return False if start2 < len(typed) or start1 < len(name) else True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        index_1, index_2 = 0, 0\n",
    "        while index_1 < len(name) and index_2 < len(typed):\n",
    "            if name[index_1] == typed[index_2]:\n",
    "                index_1 += 1\n",
    "                index_2 += 1\n",
    "            elif index_2 > 0 and typed[index_2] == typed[index_2 - 1]:\n",
    "                index_2 += 1\n",
    "            else:\n",
    "                return False\n",
    "        while 0 < index_2 < len(typed) and typed[index_2] == typed[index_2 - 1]:\n",
    "            index_2 += 1\n",
    "        if index_1 == len(name) and index_2 == len(typed):\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        idx1, idx2 = 0, 0\n",
    "        n1, n2 = len(name), len(typed)\n",
    "        while idx1<n1 and idx2<n2:\n",
    "            if name[idx1] == typed[idx2]:\n",
    "                idx1 += 1\n",
    "                idx2 += 1\n",
    "            elif idx2 > 0 and typed[idx2] == typed[idx2-1]:\n",
    "                idx2 += 1\n",
    "            else:\n",
    "                return False\n",
    "        while idx2 < n2 and typed[idx2] == typed[idx2-1]:\n",
    "            idx2 += 1\n",
    "        if idx1 == n1 and idx2 == n2:\n",
    "            return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        len_name = len(name)\n",
    "        len_typed = len(typed)\n",
    "        cnt = 0\n",
    "\n",
    "        for i in range(len_name):\n",
    "            if name[i] == typed[cnt]:\n",
    "                if (i + 1) < len_name and name[i + 1] != name[i]:\n",
    "                    while cnt < len_typed and typed[cnt] == name[i]:\n",
    "                        cnt += 1\n",
    "                else:\n",
    "                    cnt += 1\n",
    "            elif name[i] != typed[cnt]:\n",
    "                return False\n",
    "            if i < len_name - 1 and cnt >= len_typed:\n",
    "                return False\n",
    "        while cnt < len_typed and typed[cnt] == name[i]:\n",
    "            cnt += 1\n",
    "        if cnt < len_typed:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i = 0\n",
    "        j = 0\n",
    "        count_n = 0\n",
    "        count_t = 0\n",
    "        last_str_n = name[0]\n",
    "        last_str_t = typed[0]\n",
    "        #核心思想还是不断挪动指针吗，同时统计一下持续的长度，注意一下结尾怎么判断\n",
    "        while i < len(name) and j < len(typed):\n",
    "            if name[i] == typed[j]:\n",
    "                last_str_n = name[i]\n",
    "                last_str_t = typed[j]\n",
    "                count_n = 0\n",
    "                count_t = 0\n",
    "                while i < len(name) and name[i] == last_str_n:\n",
    "                    i += 1\n",
    "                    count_n += 1\n",
    "                    \n",
    "\n",
    "                while j < len(typed) and typed[j] == last_str_t:\n",
    "                    j += 1\n",
    "                    count_t += 1\n",
    "                \n",
    "                if count_n > count_t:\n",
    "                    return False\n",
    "                else:\n",
    "                    if i == len(name) and j == len(typed):\n",
    "                        break\n",
    "                    elif i == len(name) and j < len(typed) or i < len(name) and j == len(typed):\n",
    "                        return False\n",
    "                    else:\n",
    "                        continue\n",
    "            else:\n",
    "                return False\n",
    "        return True    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        #\n",
    "        i, j = 0, 0\n",
    "        while j < len(typed):\n",
    "            if i < len(name) and name[i] == typed[j]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif j > 0 and typed[j] == typed[j - 1]:\n",
    "                j += 1\n",
    "            else:\n",
    "                return False\n",
    "        return i == len(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        n1 = len(name)\n",
    "        n2 = len(typed)\n",
    "        if n2 < n1:\n",
    "            return False\n",
    "        p1 = 0\n",
    "        p2 = 0\n",
    "        pre = 0\n",
    "        while p1 < n1 or p2 < n2:\n",
    "            try:\n",
    "                if p1 >= n1 or name[p1] != typed[p2]:\n",
    "                    if typed[p2] != name[pre]:\n",
    "                        return False\n",
    "                    else:\n",
    "                        p2 += 1\n",
    "                else:\n",
    "                    pre = p1\n",
    "                    p1 += 1\n",
    "                    p2 += 1\n",
    "            except:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(name)==len(typed):\n",
    "            return name==typed\n",
    "        if len(name)>len(typed):\n",
    "            return False\n",
    "\n",
    "        i = 0\n",
    "        j = 0\n",
    "        if typed[0]!=name[0]:\n",
    "            return False\n",
    "        while i<len(name) and j <len(typed):\n",
    "            if typed[j]==name[i]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "            else:\n",
    "                if typed[j]==typed[j-1]:\n",
    "                    j+=1\n",
    "                else:\n",
    "                    print(i,j)\n",
    "                    return False\n",
    "        if typed[-1]!=name[-1]:\n",
    "            return False\n",
    "        if j!=len(typed)-1:\n",
    "            for k in range(len(typed)-1,j,-1):\n",
    "                if typed[k]!= name[-1]:\n",
    "                    print(typed[k])\n",
    "                    return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        index1 = 0\n",
    "        index2 = 0\n",
    "        while index1 < len(name) and index2 < len(typed):\n",
    "            if name[index1] == typed[index2]:\n",
    "                index1 += 1\n",
    "                index2 += 1\n",
    "            elif index2 > 0 and typed[index2 - 1] == typed[index2]:\n",
    "                index2 += 1\n",
    "            else:\n",
    "                return False\n",
    "        while 0 < index2 < len(typed) and typed[index2] == typed[index2 - 1]:\n",
    "            index2 += 1\n",
    "        if index1 == len(name) and index2 == len(typed):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        list1 = list(name)\n",
    "        list2 = list(typed)\n",
    "        if len(set(list2)) != len(set(list1)):\n",
    "            return False\n",
    "        prev = None\n",
    "        for ch in list1:\n",
    "            try:\n",
    "                index_num = list2.index(ch)\n",
    "                # print(list2[:index_num])\n",
    "                if len(set(list2[:index_num])) > 1 or (len(set(list2[:index_num])) == 1 and list2[:index_num][-1] != prev):\n",
    "                    return False\n",
    "                list2 = list2[index_num + 1:]\n",
    "                prev = ch\n",
    "            except:\n",
    "                return False\n",
    "        # print(list2)\n",
    "        if len(list2) == 0 or (len(set(list2)) == 1 and list2[-1] == list1[-1]):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        m = len(name)\n",
    "        n = len(typed)\n",
    "        i, j = 0, 0\n",
    "        while i < m and j < n:\n",
    "            if name[i] == typed[j]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                if j == 0:\n",
    "                    return False\n",
    "                while j < n and typed[j] == typed[j-1]:\n",
    "                    j += 1\n",
    "                if j == n:\n",
    "                    return False\n",
    "                if name[i] == typed[j]:\n",
    "                    i += 1\n",
    "                    j += 1\n",
    "                else:\n",
    "                    return False\n",
    "        if i < m:\n",
    "            return False\n",
    "        while j < n:\n",
    "            if typed[j] == typed[j-1]:\n",
    "                j += 1\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "            \n",
    "\n",
    "\n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        idx = 0\n",
    "        for ch in typed:\n",
    "            if idx < len(name) and ch == name[idx]:\n",
    "                idx += 1\n",
    "            elif idx - 1>= 0 and ch == name[idx - 1]:\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True if idx == len(name) else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        i = 0\n",
    "        name += \"1\"\n",
    "        for s in typed:\n",
    "            if s == name[i]:\n",
    "                i += 1\n",
    "            elif s != name[i-1]:\n",
    "                return False\n",
    "        return i == len(name) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        idx_name = 0\n",
    "        idx_typed = 0\n",
    "        name = list(name)\n",
    "        typed = list(typed)\n",
    "        while idx_name < len(name) and idx_typed < len(typed):\n",
    "            if name[idx_name] == typed[idx_typed]:\n",
    "                idx_name += 1\n",
    "                idx_typed += 1\n",
    "            else:\n",
    "                if idx_typed != len(typed)-1 and idx_typed != 0:\n",
    "                    if typed[idx_typed-1] != typed[idx_typed] and typed[idx_typed+1] != typed[idx_typed]:\n",
    "                        return False\n",
    "                elif idx_typed == len(typed)-1:\n",
    "                    if typed[idx_typed-1] != typed[idx_typed]:\n",
    "                        return False\n",
    "                elif idx_typed == 0:\n",
    "                    return False\n",
    "                idx_typed += 1\n",
    "        if idx_typed == len(typed):\n",
    "            if idx_name != len(name):\n",
    "                return False\n",
    "            else:\n",
    "                return True\n",
    "        else:\n",
    "            for i in range(idx_typed, len(typed)):\n",
    "                if typed[i] != typed[idx_typed-1]:\n",
    "                    return False\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        l,r=0,0\n",
    "        while l<len(name) and r<len(typed):\n",
    "            target=name[l]\n",
    "            pre_l,pre_r=l,r\n",
    "            while l<len(name) and name[l]==target:\n",
    "                l+=1\n",
    "            if typed[r]!=target:\n",
    "                return False\n",
    "            while r<len(typed) and typed[r]==target:\n",
    "                r+=1\n",
    "            if l-pre_l>r-pre_r:\n",
    "                return False\n",
    "        return l==len(name) and r==len(typed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        # 初始化两个指针，分别指向name和typed\n",
    "        i, j = 0, 0\n",
    "\n",
    "        # 遍历typed\n",
    "        while j < len(typed):\n",
    "            if i < len(name) and typed[j] == name[i]:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            # 如果当前字符与前一个字符相等，说明可能是长按键入\n",
    "            elif j > 0 and typed[j] == typed[j - 1]:\n",
    "                j += 1\n",
    "            else:\n",
    "                # 不匹配，返回False\n",
    "                return False\n",
    "\n",
    "        # 检查name是否遍历完\n",
    "        return i == len(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        if len(name)>len(typed):\n",
    "            return False\n",
    "        elif name == typed:\n",
    "            return True\n",
    "        name = list(name)+['-']*(len(typed)-len(name))\n",
    "        typed = list(typed)\n",
    "        i = 1\n",
    "        if name[0]!=typed[0]:\n",
    "            return False\n",
    "        while i in range(1,len(typed)):\n",
    "            if name[i]!=typed[i] and typed[i]==typed[i-1]:\n",
    "                name.insert(i,typed[i])\n",
    "                if name[-1] == '-':\n",
    "                    del name[-1]\n",
    "            i = i+1\n",
    "        \n",
    "        if name == typed:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "     \n",
    "        if name[0] != typed[0]:\n",
    "            return False\n",
    "\n",
    "        start1, start2 = 0, 0\n",
    "        cmp = name[0]\n",
    "\n",
    "        while start1 < len(name) and start2 < len(typed):\n",
    "            cnt1, cnt2 = 0, 0\n",
    "            \n",
    "            while start1 < len(name) and name[start1] == cmp:\n",
    "                    start1 += 1\n",
    "                    cnt1 += 1\n",
    "            \n",
    "            while start2 < len(typed) and typed[start2] == cmp:\n",
    "                    start2 += 1\n",
    "                    cnt2 += 1\n",
    "\n",
    "            if start1 < len(name) and start2 < len(typed) and name[start1] != typed[start2]:\n",
    "                return False\n",
    "\n",
    "            if cnt1 > cnt2:\n",
    "                return False\n",
    "\n",
    "            if start1 < len(name):\n",
    "                cmp = name[start1] \n",
    "            \n",
    "        return False if start2 < len(typed) or start1 < len(name) else True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        #### 直接使用区间法\n",
    "        fast = 0\n",
    "        pre_fast = 0\n",
    "        slow = 0\n",
    "        pre_slow = 0\n",
    "        while True:\n",
    "            while fast < len(name):\n",
    "                if name[fast] == name[pre_fast]:\n",
    "                    fast += 1\n",
    "                else:\n",
    "                    break\n",
    "            while slow < len(typed):\n",
    "                if typed[slow] == typed[pre_slow]:\n",
    "                    slow += 1\n",
    "                else:\n",
    "                    break\n",
    "            if fast - pre_fast <= slow - pre_slow and name[pre_fast] == typed[pre_slow]:\n",
    "                pre_fast = fast\n",
    "                pre_slow = slow\n",
    "            else:\n",
    "                return False\n",
    "            if fast < len(name) and slow < len(typed):\n",
    "                continue\n",
    "            if fast > len(name)-1 and slow > len(typed)-1:\n",
    "                return True\n",
    "            \n",
    "\n",
    "            return False\n",
    "            \n",
    "\n",
    "            \n",
    "        \n",
    "                        \n",
    "                            \n",
    "                        \n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        n1=len(name)\n",
    "        n2=len(typed)\n",
    "        i = j= 0\n",
    "        res=False\n",
    "        while i < n1 and j < n2:\n",
    "            if  name[i] == typed[j]:\n",
    "                i+=1\n",
    "                j+=1\n",
    "            else:\n",
    "                if j==0:return False\n",
    "                while j < n2-1 and typed[j]==typed[j-1]:\n",
    "                    j+=1\n",
    "                if i<n1 and j <n2 and name[i]==typed[j]:\n",
    "                    i+=1\n",
    "                    j+=1\n",
    "                else:\n",
    "                    return False\n",
    "\n",
    "        if i<n1 :\n",
    "            return False\n",
    "        while j<n2:\n",
    "            if typed[j]==typed[j-1]:\n",
    "                j+=1\n",
    "            else:\n",
    "                return False\n",
    "        return True "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        # 分离双指针\n",
    "        index1, index2 = 0, 0\n",
    "        n1, n2 = len(name), len(typed)\n",
    "        if n1 == 1 and n2 == 1: return name[index1] == typed[index2]\n",
    "        if n2 < n1: return False\n",
    "        if name[0] != typed[0]: return False\n",
    "        while index2 < n2:\n",
    "            if index1 != n1 and name[index1] == typed[index2]: \n",
    "                index1 += 1\n",
    "                index2 += 1\n",
    "            else:\n",
    "                if typed[index2] == typed[index2-1]:\n",
    "                    index2 += 1\n",
    "                else:\n",
    "                    return False\n",
    "        if index1 != n1: return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        一号 = 0\n",
    "        二号 = 0\n",
    "        while 二号 < len(typed):\n",
    "            if 一号 < len(name) and name[一号] == typed[二号]:\n",
    "                一号 += 1\n",
    "                二号 += 1\n",
    "            elif 二号 > 0 and typed[二号] == name[一号 - 1]:  # 这里也可以用  “一号 > 0” 或者 官方的 \"typed[二号] == typed[二号 - 1]\"\n",
    "                二号 += 1\n",
    "            else:\n",
    "                return False\n",
    "        return 一号 == len(name)\n",
    "\n",
    "\n",
    "# 作者：Flying_Du\n",
    "# 链接：https://leetcode.cn/problems/long-pressed-name/solutions/454392/python-hao-peng-you-shou-la-shou-yi-qi-zou-quan-gu/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        p = 0\n",
    "        q = 0\n",
    "\n",
    "        m = len(name)\n",
    "        n = len(typed)\n",
    "        # 遍历 typed 与 name 中的字符比较\n",
    "        while q < n:\n",
    "            # 比较，相同移动指针\n",
    "            if p < m and name[p] == typed[q]:\n",
    "                p += 1\n",
    "                q += 1\n",
    "            # 不相同时，要注意 p 指针指向的元素\n",
    "            # 如果是首元素，那么表示 name 和 typed 首字符都不同，可以直接返回 False\n",
    "            # 如果不在首元素，看是否键入重复，键入重复，继续移动 q 指针，继续判断；如果不重复，也就是不相等的情况，直接返回 False，表示输入错误\n",
    "            elif p > 0 and name[p-1] == typed[q]:\n",
    "                q += 1\n",
    "            else:\n",
    "                return False\n",
    "        \n",
    "        # typed 遍历完成后要检查 name 是否遍历完成\n",
    "        return p == m\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        left_1, left_2 = 0, 0\n",
    "\n",
    "        while left_1 < len(name) and left_2 < len(typed):\n",
    "            if name[left_1] == typed[left_2]:\n",
    "                left_1 += 1\n",
    "                left_2 += 1\n",
    "            elif left_2 > 0 and typed[left_2 - 1] == typed[left_2]:\n",
    "                left_2 += 1\n",
    "            else:\n",
    "                return False\n",
    "        while 0 < left_2 < len(typed) and typed[left_2] == typed[left_2 - 1]:\n",
    "            left_2 += 1\n",
    "\n",
    "        if left_1 == len(name) and left_2 == len(typed):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        a,b = 0,0\n",
    "        m,n = len(name),len(typed)\n",
    "        while a < m and b < n:\n",
    "            c = name[a]\n",
    "            e,f = 0,0\n",
    "            while name[a] == c:\n",
    "                a += 1\n",
    "                e += 1\n",
    "                if a == m:\n",
    "                    break\n",
    "            while typed[b] == c:\n",
    "                b += 1\n",
    "                f += 1\n",
    "                if b == n:\n",
    "                    break\n",
    "            if e > f:\n",
    "                return False\n",
    "        if a != m or b != n:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLongPressedName(self, name: str, typed: str) -> bool:\n",
    "        g1 = [(s, len(list(grp))) for s, grp in groupby(name)]\n",
    "        g2 = [(s, len(list(grp))) for s, grp in groupby(typed)]\n",
    "        if len(g1) != len(g2):\n",
    "            return False\n",
    "        for i in range(len(g1)):\n",
    "            if g1[i][0] != g2[i][0] or g1[i][1] > g2[i][1]:\n",
    "                return False\n",
    "        return True"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
